package com.zhouyibike.uu.ui.activity.yihoudong;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.entity.GetYiTransferResult;
import com.zhouyibike.uu.entity.YiHuoDongResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.listener.MyListener;
import com.zhouyibike.uu.ui.activity.web.TotalWebActivity;
import com.zhouyibike.uu.ui.adapter.YihuodongAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.PullToRefreshLayout;
import com.zhouyibike.uu.ui.view.TXCircleImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YihuodongActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle,shiyongshuoming;
    private ListView lv_allmsg;
    private int refreshtype = 0;
    private int page = 1;//页码
    private boolean ismore = true;//判断是否还有更多数据
    private List<GetYiTransferResult.DataBean> alllist = new ArrayList<>();
    private YihuodongAdapter mydetailAdapter;
    private PullToRefreshLayout pullToRefreshLayout;
    private LinearLayout  yiquanshuoming;
    private TXCircleImageView mCircleImageView;
    private TextView tvbuying,tvbuy, tvsell, tvname, buyyijuanbt, sellyijjuanbt, yihuodong_mynum,noyiquantv,yiquanmingxibt;
    private int cansellyiquan = 0;
    private RelativeLayout ll_nothing,rl, rll;
    private ProgressBar loading;
    private ImageView noyiquaniv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yihuodong);
        initView();
        getYihuodonginfo();
        getMsg();
        initListener();
    }

    private void initListener() {
        mBtnBack.setOnClickListener(this);
        buyyijuanbt.setOnClickListener(this);
        sellyijjuanbt.setOnClickListener(this);
        yiquanshuoming.setVisibility(View.GONE);
        yiquanshuoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWZMsg();
            }
        });
        shiyongshuoming= (TextView) findViewById(R.id.shiyongshuoming);
        shiyongshuoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWZMsg();
            }
        });
    }

    private void initView() {
        //  rll= (RelativeLayout) findViewById(R.id.rll);
        yiquanmingxibt= (TextView) findViewById(R.id.yiquanmingxibt);
        noyiquantv= (TextView) findViewById(R.id.noyiquantv);
        noyiquaniv= (ImageView) findViewById(R.id.noyiquantu);
        loading= (ProgressBar) findViewById(R.id.loading);
        yihuodong_mynum = (TextView) findViewById(R.id.yihuodong_mynum);
        rl = (RelativeLayout) findViewById(R.id.rl);
        yiquanshuoming = (LinearLayout) findViewById(R.id.yiquanshuoming);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("翼活动");
        lv_allmsg = (ListView) findViewById(R.id.content_view);
//        LayoutInflater inflater = LayoutInflater.from(this);
//        View head = inflater.inflate(R.layout.nav_header_main_new, lv_allmsg, false);
//        lv_allmsg.addHeaderView(head);
        pullToRefreshLayout = ((PullToRefreshLayout) findViewById(R.id.refresh_view));
        pullToRefreshLayout.setOnRefreshListener(new YihuodongActivity.MyListener2());
        ll_nothing = (RelativeLayout) findViewById(R.id.ll_nothing_show);
        mCircleImageView = (TXCircleImageView) findViewById(R.id.yihuodong_userIcon);
        tvbuy = (TextView) findViewById(R.id.yihuodong_buy);
        tvbuying= (TextView) findViewById(R.id.yihuodong_buying);
        tvsell = (TextView) findViewById(R.id.yihuodong_sell);
        tvname = (TextView) findViewById(R.id.yihuodong_username);
        buyyijuanbt = (TextView) findViewById(R.id.yihuodong_buybt);
        sellyijjuanbt = (TextView) findViewById(R.id.yihuodong_sellbt);
        yiquanmingxibt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(YihuodongActivity.this,MyYiquanmingxiActivity.class));
            }
        });
    }

    /**
     * //获取翼活动交易记录
     */
    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("pageIndex", page + "");
//        if (page == 1 && refreshtype == 0)
//            showProgressDialog();
        addSubscription(apiStores.GetYiTransfer(params),
                new ApiCallback<GetYiTransferResult>() {

                    @Override
                    public void onSuccess(GetYiTransferResult model) {
//                        String string=new Gson().toJson(model);
//                        Log.e("onSuccess: ",string );
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            if (refreshtype == 1) {
                                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                alllist.clear();
                            } else if (refreshtype == 2) {
                                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            }
                            if (model.getData().size() == 0) {
                                ismore = false;
                            }
//                            for (int i = 0; i <10 ; i++) {
//                                alllist.addAll(model.getData());
//                            }
                            alllist.addAll(model.getData());

                            if (alllist.size() == 0) {
                                rl.setVisibility(View.GONE);
                                pullToRefreshLayout.setVisibility(View.INVISIBLE);
                                ll_nothing.setVisibility(View.VISIBLE);
                            } else {
                                rl.setVisibility(View.VISIBLE);
                                pullToRefreshLayout.setVisibility(View.VISIBLE);
                                ll_nothing.setVisibility(View.GONE);
                            }
                            initData();

                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
//                        rl.setVisibility(View.GONE);
//                        pullToRefreshLayout.setVisibility(View.INVISIBLE);

                    }

                    @Override
                    public void onFinish() {
                        loading.setVisibility(View.GONE);
                        noyiquantv.setVisibility(View.VISIBLE);
                        noyiquaniv.setVisibility(View.VISIBLE);
                       // ll_nothing.setVisibility(View.GONE);
                      //  dismissProgressDialog();
                    }
                });
    }

    /**
     * 获取用户自己的翼券数量，翼券市场
     */
    private void getYihuodonginfo() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
//        showProgressDialog();
        addSubscription(apiStores.YiHuoDong(params),
                new ApiCallback<YiHuoDongResult>() {
                    @Override
                    public void onSuccess(YiHuoDongResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            initData2(model);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        yihuodong_mynum.setText("" + 0);
                        tvsell.setText("可转让\n" + 0);
                        tvbuy.setText("可使用\n" + 0);
                        tvbuying.setText("抢购中\n" +0);
                        tvname.setText(preferences.getString("mobile", ""));
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
//                        dismissProgressDialog();
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.yihuodong_buybt:
                Intent intent = new Intent(YihuodongActivity.this, YijuangounaiActivity.class);//跳转购买100
                startActivityForResult(intent, 100);
                break;
            case R.id.yihuodong_sellbt:
                Intent intent2 = new Intent(YihuodongActivity.this, YihuodongzhuanrangActivity.class);//跳转转让200
                intent2.putExtra("cansellnum", cansellyiquan);
                startActivityForResult(intent2, 200);
                break;
            default:
                break;
        }
    }

    private void initData() {
        if (page == 1) {
            mydetailAdapter = new YihuodongAdapter(this, alllist);
            lv_allmsg.setAdapter(mydetailAdapter);
        } else {
            mydetailAdapter.setList(alllist);
        }
    }

    private void initData2(YiHuoDongResult model) {
        cansellyiquan = model.getData().getTransferAmount();
//        yihuodong_mynum.setText("我的翼券 " + model.getData().getUsegeAmount());
//        tvsell.setText("可转让 " + model.getData().getTransferAmount());
//        tvbuy.setText("可使用 " + model.getData().getAmount());
        yihuodong_mynum.setText("" + model.getData().getAmount());
        tvsell.setText("可转让\n" + model.getData().getTransferAmount());
        tvbuy.setText("可使用\n" + model.getData().getUsegeAmount());
        tvbuying.setText("抢购中\n" + model.getData().getbuyingAmount());
        tvname.setText(preferences.getString("mobile", ""));
//        if (!TextUtils.isNullorEmpty(model.getData().getHeadImg())) {
////            http://img.ivsky.com/img/tupian/pre/201104/16/anhui_huangshan_fengjing-055.jpg
//            Picasso.with(this).load(model.getData().getHeadImg()) .transform(ImgUtils.Picassocompress(mCircleImageView)).error(R.mipmap.head_moren).placeholder(R.mipmap.head_moren).networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE).into(mCircleImageView);
//        }
        //   Picasso.with(this).load("http://img.ivsky.com/img/tupian/pre/201104/16/anhui_huangshan_fengjing-055.jpg").into(mCircleImageView);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 1) {
            refreshtype = 0;
            page = 1;//页码
            alllist.clear();
            getYihuodonginfo();
            getMsg();
        }
        if (requestCode == 200 && resultCode == 1) {
            refreshtype = 0;
            page = 1;//页码
            alllist.clear();
            getYihuodonginfo();
            getMsg();
        }
    }

    class MyListener2 extends MyListener {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            super.onRefresh(pullToRefreshLayout);
            refreshtype = 1;
            page = 1;
            ismore = true;
            getMsg();
            getYihuodonginfo();
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            super.onLoadMore(pullToRefreshLayout);
            refreshtype = 2;
            getYihuodonginfo();
            if (ismore) {
                page++;
                getMsg();
            } else {
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }
    }

    public void setgone(boolean bb) {
        if (bb) {
            rll.setVisibility(View.GONE);
            rl.setVisibility(View.VISIBLE);
        } else {
            rl.setVisibility(View.GONE);
            rll.setVisibility(View.VISIBLE);
        }
    }

    private void getWZMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("type", "8");
        showProgressDialog();
        addSubscription(apiStores.GetInformationzcxy(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            Intent i = new Intent(YihuodongActivity.this, TotalWebActivity.class);
                            i.putExtra("title", "翼券使用说明");
                            i.putExtra("url", model.getData());
                            startActivity(i);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
}
